<template>
  <el-row>
    <el-col :span="4">
      <el-tree :data="rootData" ref="tree" :props="defaultProps" lazy :load="loadNode" @node-click="handleNodeClick"></el-tree>
    </el-col>
    <el-col :span="20">
    </el-col>
  </el-row>
</template>

<script>
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {getBuildingList} from "@/api/building";
import {getInRegionList} from "@/api/region";
import {getEquipmentList} from "@/api/equipment";

export default {
  name: "gridList",
  components: {
  },
  data() {
    return {
      defaultProps: {
        label: 'name',
        isLeaf: 'leaf',
        children: 'children'
      },
      rootData: []
    };
  },
  created() {
    this.getEnterpriseList()
  },
  methods: {
    //数据懒加载
    loadNode(node, resolve) {
      //加载安全主体数据
      if (node.level === 0) {
        return resolve(this.rootData)
      }
      let data;
      //type:1 主体；2 建筑； 3 楼层； 4 区域；5 设备
      switch (node.data.type) {
        case 1:
          data = this.getBuildingList(node.data.ucId)
          break
        case 3:
          data = this.getRegionList(node.data.ucId, node.data.floor)
          break
        case 4:
          data = this.getEquipmentList(node.data.ucId)
          break
      }
      if (data) {
        data.then(list => {
          resolve(list);
        })
      } else {
        return resolve([])
      }
    },
    handleNodeClick(data) {
      console.log(data);
      //type:1 主体；2 建筑； 3 楼层； 4 区域；5 设备
      switch (data.type) {
        case 1:
          // this.$router.push("/enterprise/enterpriseinfo/" + data.ucId)
          break
        case 2:
        case 3:
          this.$router.push("/enterprise/building/info?ucId=" + data.ucId)
          break
        case 4:
          this.$router.push("/enterprise/region/info?ucId=" + data.ucId)
          break
        case 5:
          this.$router.push("/equipment/equipmentInfo/" + data.ucId)
          break
      }
    },
    //获取主体数据列表
    getEnterpriseList() {
      getInEnterpriseDominantList({dominantType: 6}).then(res => {
        //封装安全主体
        res.data.forEach(d => {
          const org = {ucId: d.ucId, type: 1, name: d.name}
          this.rootData.push(org)
        })
      })


    },
    //根据主体uuid获取建筑列表
    getBuildingList(val) {
      return getBuildingList({deleted: false, enterpriseId: val}).then(res => {
        let buildList = []
        res.data.forEach(d => {
          //封装建筑
          let org = {ucId: d.ucId, type: 2, name: d.name, leaf: true}
          let childrenList = []
          //地下层数
          if (d.underFloorNum > 0) {
            for (let i = d.underFloorNum; i > 0; i--) {
              childrenList.push({ucId: d.ucId, type: 3, name: "第" + (-i) + "层", floor: -i, leaf: true})
            }
          }
          //地上层数
          if (d.overFloorNum > 0) {
            for (let i = 1; i <= d.overFloorNum; i++) {
              childrenList.push({ucId: d.ucId, type: 3, name: "第" + i + "层", floor: -i, leaf: true})
            }
          }
          org.children = childrenList
          buildList.push(org)
        })
        return buildList
      })
    },
    //根据建筑uuid和层数获取区域
    getRegionList(val, floorNum) {
      //封装区域数据
      return getInRegionList({buildId: val, floor: floorNum}).then(res => {
        let regionList = []
        res.data.forEach(d => {
          regionList.push({ucId: d.ucId, type: 4, name: d.name, leaf: true})
        })
        return regionList
      })
    },
    //根据建筑uuid和层数获取区域
    getEquipmentList(val) {
      //封装区域数据
      return getEquipmentList({regionId: val}).then(res => {
        let equipmentList = [];
        res.data.forEach(d => {
          equipmentList.push({ucId: d.ucId, type: 5, name: d.name, leaf: false})
        })
        return equipmentList
      })
    }
  }
}
</script>
